package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.hencoder.hencoderpracticedraw1.R;

public class Practice11PieChartView extends View {

    private final float locat=200;

    private float mRadius=200;   //半径
    private float diam=200*2;       //直径

    private float mLeft=500;

    private float mTop=500;
    private RectF rectF=new RectF(mLeft,mTop,mLeft+diam,mTop+diam);

    private float stopAngle=160;            //每个饼图结束的角度

    private Canvas canvas;

    public Practice11PieChartView(Context context) {
        super(context);
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice11PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas=canvas;

//        综合练习
//        练习内容：使用各种 Canvas.drawXXX() 方法画饼图

//        drawWhiteCircle(canvas);

        drawPie(Color.RED,150,0,-20);
        drawPie(Color.YELLOW,50,0);
        drawPie(getResources().getColor(R.color.color_9B24AF),10,5);
        drawPie(getResources().getColor(R.color.color_9F9F9F),7,5);
        drawPie(Color.GREEN,45);
        drawPie(Color.BLUE,80,5);
    }


    //先画一个参照物 圆

    private void drawWhiteCircle(Canvas canvas){

        Paint paint=new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        canvas.drawArc(rectF,0,360,false,paint);

    }


    private void drawRed(Canvas canvas){


        Paint paint=new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawArc(rectF,160,150,true,paint);


    }


    private void drawYellow(Canvas canvas){


        Paint paint=new Paint();
        paint.setColor(Color.YELLOW);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawArc(rectF,310,30,true,paint);

    }


    /**
     * 画其他饼图
     * @param color  颜色
     * @param mAngle  饼图的度数

     */
    private void drawPie(int color,float mAngle){
        float mLeft=locat;
        float mTop=locat;
        RectF rectF=new RectF(mLeft,mTop,mLeft+diam,mTop+diam);
        Paint paint=new Paint();
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawArc(rectF,stopAngle+3,mAngle,true,paint);
        stopAngle=stopAngle+3+mAngle;

    }

    /**
     * 画其他饼图
     * @param color  颜色
     * @param mAngle  饼图的度数
     * @param space     扇形与扇形的间隔
     */
    private void drawPie(int color,float mAngle,float space){
        float mLeft=locat;
        float mTop=locat;
        RectF rectF=new RectF(mLeft,mTop,mLeft+diam,mTop+diam);
        Paint paint=new Paint();
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawArc(rectF,stopAngle+space,mAngle,true,paint);
        stopAngle=stopAngle+space+mAngle;

    }

    /**
     * 画其他饼图
     * @param color  颜色
     * @param mAngle  饼图的度数
     * @param space     扇形与扇形的间隔
     * @param move   饼图与其他饼图分离的间隔
     */
    private void drawPie(int color,float mAngle,float space,float move){
        float mLeft=locat;
        float mTop=locat;
        mLeft+=move;
        mTop+=move;
        RectF rectF=new RectF(mLeft,mTop,mLeft+diam,mTop+diam);
        Paint paint=new Paint();
        paint.setColor(color);
        paint.setStyle(Paint.Style.FILL);

        canvas.drawArc(rectF,stopAngle+space,mAngle,true,paint);
        stopAngle=stopAngle+space+mAngle;
    }



}
